﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProblemScore
{
    
    public partial class Form1 : Form
    {
        int timeLeft = 5;
        int Score = 0;
        int num1 = 0;
        int num2 = 0;
        int problem_num =5;
        string ope = "+";
        private void timer_Tick(object sender, EventArgs e)
        {
            timeLeft--;
            labelTime.Text = timeLeft.ToString() ;

            if (timeLeft == -1)
            {
                //时间结束,切换下一题
                GenerateNextQuestion();
            }
        }
        private void CheckAnswer()
        {
            string resultText;
            timerb.Stop();
            if (ope == "-") { 
            if (textBox1.Text == (num1-num2).ToString())
            {
                //答对处理
               MessageBox.Show("回答正确!");

                Score = Score + 5;
            }
            else
            {
                //答错处理  
                MessageBox.Show("回答错误,正确答案是:" + (num1 - num2)); 
            }
            }
            else
            {
                if (textBox1.Text == (num1 + num2).ToString())
                {
                    //答对处理
                    MessageBox.Show("回答正确!");

                    Score = Score + 5;
                }
                else
                {
                    //答错处理  
                    MessageBox.Show("回答错误,正确答案是:" + (num1 + num2));
                }
            }
            //显示结果提示


            //切换到下一题
            GenerateNextQuestion();

            

           
        }
        Timer timerb = new Timer() { Interval = 1000 };
      
        private void GenerateNextQuestion()
        {
            if ((5 - problem_num + 1) < 6)
            {
                label12.Text = (5 - problem_num + 1).ToString();
                
            }
            else
                label12.Text = "5";
            problem_num = problem_num - 1;
            if (problem_num == -1)
            {
                timerb.Stop();
                labelTime.Text = "0";
                MessageBox.Show("您的总分是" + Score);
                button1.Enabled = false;
                textBox1.Enabled = false;
                label12.Text = "5";
            }

            Random random = new Random();
            num1 = random.Next(1, 39);
            num2 = random.Next(1, 20);
            Random operatorGenerator = new Random();
            int operatorIndex = operatorGenerator.Next(0, 2);

            if (operatorIndex == 0)
            {
                ope = "+";
            }
            else
            {
                 ope = "-";
            }

            //显示在Label上
            label1.Text = num1.ToString();
            label2.Text = ope.ToString();
            label3.Text = num2.ToString();
            //重置答案输入   
            textBox1.Text = "";

            //记录题目数据
            
            
           if (problem_num != -1) { 
                timerb.Start();
                timeLeft = 5;
                labelTime.Text = timeLeft.ToString() ;
               
            }
            //启动Timer继续计时

        }
        public Form1()
        {
            InitializeComponent();

            GenerateNextQuestion();
            timerb.Tick += timer_Tick;
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            CheckAnswer();
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void label9_Click(object sender, EventArgs e)
        {

        }

        private void label8_Click(object sender, EventArgs e)
        {

        }
    }

}
